package com.ex.ui.questionpanel;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import java.awt.SystemColor;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;

import com.ex.shapecalc.CircleQuestion;
import com.ex.ui.infoframe.PersonInfoIFrame;

import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class CircleAreaPanel extends JPanel {
	 private JButton mCommitBtn;
	 private JLabel mRightAnswerTa;
	 private JTextField mPerimeterTf;
	 private JTextField mAreaTf;
	 private JCheckBox mAuto;
	 private JTextField mRTf;
	 private PersonInfoIFrame mScoreFrame;
	 
	 private CircleQuestion mCircleQ;
	public CircleAreaPanel(PersonInfoIFrame f) {
		mScoreFrame = f;
		 initialize();
		 mCircleQ = new CircleQuestion();
		 mRTf.setText(String.valueOf(mCircleQ.getmCircler()));
		 
	
		 
	}
	
	public void setScore(boolean a, boolean g)
	{
		if (mScoreFrame!=null)
		{
			if(a&g)
				mScoreFrame.addQustion(true);
			else
			mScoreFrame.addQustion(false);
		}
	}
	private void initialize() {
		setToolTipText("园的面积计算");
		this.setBounds(100, 100, 444, 278);
		setLayout(null);
		
		JLabel label = new JLabel("园的面积");
		label.setBounds(60, 114, 72, 18);
		add(label);
		
		JTextArea txtrpi = new JTextArea();
		txtrpi.setRows(2);
		txtrpi.setWrapStyleWord(true);
		txtrpi.setText("已知园的半径，求园的面积和周长。\r\npi = 3.14，结果保留2位小数。");
		txtrpi.setOpaque(false);
		txtrpi.setForeground(Color.BLUE);
		txtrpi.setFont(new Font("宋体", Font.BOLD, 16));
		txtrpi.setBackground(SystemColor.inactiveCaptionBorder);
		txtrpi.setBounds(60, 13, 304, 42);
		add(txtrpi);
		
		JLabel label_1 = new JLabel("题目：");
		label_1.setBounds(14, 16, 45, 18);
		add(label_1);
		
		JTextArea txtrr = new JTextArea();
		txtrr.setWrapStyleWord(true);
		txtrr.setText("半径R为：");
		txtrr.setOpaque(false);
		txtrr.setForeground(Color.BLUE);
		txtrr.setFont(new Font("宋体", Font.BOLD, 16));
		txtrr.setBackground(SystemColor.inactiveCaptionBorder);
		txtrr.setBounds(70, 68, 93, 23);
		add(txtrr);
		
		mRTf = new JTextField();
		mRTf.setEnabled(false);
		mRTf.setName("mRTf");
		mRTf.setColumns(10);
		mRTf.setBounds(158, 68, 35, 24);
		add(mRTf);
		
		mAuto = new JCheckBox("自动出题");
		mAuto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (mAuto.isSelected()) {
					mCircleQ.createQuestion();
					 mRTf.setText(String.valueOf(mCircleQ.getmCircler()));
					 mRTf.setEnabled(false);
					mRightAnswerTa.setVisible(false);
				} else {
					mRTf.setEnabled(true);
				}
				
			}
		});
		mAuto.setSelected(true);
		mAuto.setName("mAuto");
		mAuto.setBounds(214, 110, 133, 27);
		add(mAuto);
		
		JLabel label_2 = new JLabel("答案：");
		label_2.setBounds(14, 114, 45, 18);
		add(label_2);
		
		mCommitBtn = new JButton("提交");
		mCommitBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			 
				if (!mAuto.isSelected()) {
					int r = Integer.valueOf(mRTf.getText()); 
					mCircleQ.createQuestion(r);
				}
				double a = Float.valueOf(mAreaTf.getText());
				double p = Float.valueOf(mPerimeterTf.getText());

				boolean aright = mCircleQ.setAreaAnswer(a);
				boolean pright = mCircleQ.setPerimeterAnswer(p);

				String ra = String.format("%4.2f", mCircleQ.getRightArea()); 
				String rp = String.format("%4.2f", mCircleQ.getRightPerimeter());
			  	if (!(aright | pright))
				{
					mRightAnswerTa.setText("回答错误！\n面积为：" +
							ra+ "周长为：" + 
							rp);
					mRightAnswerTa.setForeground(Color.red);
					 
				}
				else 	if (aright & pright) 
				{
					mRightAnswerTa.setText("回答正确！");
					mRightAnswerTa.setForeground(Color.green);
					 
					
				}else 	if (aright) {
					mRightAnswerTa.setText("回答面积正确，周长错误！\n面积为：" +
							ra+ "周长为：" + 
							rp);
					mRightAnswerTa.setForeground(Color.red);
					 
				}else
				{
					mRightAnswerTa.setText("回答周长正确，面积错误！\n面积为：" +
							ra+ "周长为：" + 
							rp);
					mRightAnswerTa.setForeground(Color.red); 
				}
				mRightAnswerTa.setVisible(true);
				setScore(aright, pright);
			}
		});
		mCommitBtn.setName("mCommitBtn");
		mCommitBtn.setBounds(224, 149, 63, 27);
		add(mCommitBtn);
		
		mAreaTf = new JTextField();
		mAreaTf.setName("mAreaTf");
		mAreaTf.setColumns(10);
		mAreaTf.setBounds(129, 111, 55, 24);
		add(mAreaTf);
		
		JLabel label_3 = new JLabel("园的周长");
		label_3.setBounds(60, 153, 72, 18);
		add(label_3);
		
		mPerimeterTf = new JTextField();
		mPerimeterTf.setName("mPerimeterTf");
		mPerimeterTf.setColumns(10);
		mPerimeterTf.setBounds(129, 150, 55, 24);
		add(mPerimeterTf);
		
		mRightAnswerTa = new JLabel();
		mRightAnswerTa.setVerticalAlignment(SwingConstants.TOP);
		mRightAnswerTa.setBounds(14, 189, 416, 61); 
		mRightAnswerTa.setText("正确答案：\r\n\r\n\r\n");
		mRightAnswerTa.setForeground(Color.RED);
		mRightAnswerTa.setFont(new Font("Monospaced", Font.BOLD, 17));
		this.add(mRightAnswerTa);
		
		 JButton mNextBtn = new JButton("出题");
		 mNextBtn.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent e) {
		 		mCircleQ.createQuestion();
				 mRTf.setText(String.valueOf(mCircleQ.getmCircler()));
				 mRTf.setEnabled(false);
		 	}
		 });
		 mNextBtn.setName("mNextBtn");
		 mNextBtn.setBounds(301, 149, 63, 27);
		 add(mNextBtn);
	}
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CircleAreaPanel p = new CircleAreaPanel(null);
					JFrame window = new JFrame();
					 window.setContentPane(p);					 
					 window.setSize( p.getWidth(), p.getHeight());
					 window.setLocationRelativeTo(null);
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	} 
}
